package cn.edu.scau.cmi.ema.controller;

import cn.edu.scau.cmi.ema.base.CmiPagination;
import cn.edu.scau.cmi.ema.base.CmiEmaController;
import cn.edu.scau.cmi.ema.domain.Crawler;
import cn.edu.scau.cmi.ema.domain.Crawlerpicture;
import cn.edu.scau.cmi.ema.util.CmiHibernateProxyUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Set;

@Controller("CrawlerpictureController")
public class CrawlerpictureController extends CmiEmaController {

	@RequestMapping("/listCrawlerpictures")
	public ModelAndView listCrawlerpictures(HttpServletRequest request, @ModelAttribute CmiPagination pagination) {
		ModelAndView mav = new ModelAndView();

		pagination = crawlerpicturePaginationService.updatePagination(request, pagination);
		Set<Crawlerpicture> crawlerpictures = crawlerpicturePaginationService.getPaginationCrawlerpictures(pagination);

		mav.addObject("crawlerpictures", crawlerpictures);
		mav.addObject("entityName", "crawlerpicture");
		mav.addObject("pagination", pagination);
		mav.setViewName("crawlerpicture/listCrawlerpictures");
		return mav;
	}

	public String listCrawlerpictures() {
		return "redirect:/listCrawlerpictures";
	}

	@RequestMapping("/viewCrawlerpicture")
	public ModelAndView viewCrawlerpicture(@RequestParam Integer crawlerpictureId) {
		ModelAndView mav = new ModelAndView();
		mav.addObject("crawlerpicture", crawlerpictureDAO.findCrawlerpictureByPrimaryKey(crawlerpictureId));
		mav.setViewName("crawlerpicture/viewCrawlerpicture");
		return mav;
	}

	@RequestMapping(value = "/confirmDeleteCrawlerpictures")
	public ModelAndView confirmDeleteCrawlerpictures(
			@RequestParam(value = "deleteCrawlerpicturesIds", required = true) Set<Integer> deleteCrawlerpicturesIds) {
		ModelAndView mav = new ModelAndView();

		Set<Crawlerpicture> deleteCrawlerpictures = crawlerpictureDAO
				.findCrawlerpicturesByIds(deleteCrawlerpicturesIds);
		Set<Crawlerpicture> referencedCrawlerpictures = crawlerpictureService
				.loadReferenceCrawlerpictures(deleteCrawlerpictures);
		boolean hasReferenced;
		if (referencedCrawlerpictures.size() == 0) {
			mav.addObject("deleteEntitys", deleteCrawlerpictures);
			hasReferenced = false;
		} else {
			deleteCrawlerpictures.removeAll(referencedCrawlerpictures);
			mav.addObject("referencedEntitys", referencedCrawlerpictures);
			mav.addObject("deleteEntitys", deleteCrawlerpictures);
			hasReferenced = true;
		}
		mav.addObject("entityName", "Crawlerpicture");
		mav.addObject("hasReferenced", hasReferenced);
		mav.setViewName("forward:/listCrawlerpictures");
		return mav;
	}

	@RequestMapping("/deleteCrawlerpicture")
	public String deleteCrawlerpicture(@RequestParam Integer crawlerpictureId) {
		Crawlerpicture crawlerpicture = crawlerpictureDAO.findCrawlerpictureByPrimaryKey(crawlerpictureId);
		crawlerpictureService.deleteCrawlerpicture(crawlerpicture);
		return "forward:/listCrawlerpictures";
	}

	@RequestMapping("/confirmDeleteCrawlerpicture")
	public ModelAndView confirmDeleteCrawlerpicture(@RequestParam Integer crawlerpictureId) {

		ModelAndView mav = new ModelAndView();

		mav.addObject("crawlerpicture", crawlerpictureDAO.findCrawlerpictureByPrimaryKey(crawlerpictureId));

		mav.setViewName("crawlerpicture/deleteCrawlerpicture");

		return mav;
	}

	@RequestMapping("/deleteCrawlerpictures")
	public String deleteCrawlerpictures(
			@RequestParam(value = "deleteCrawlerpicturesId", required = true) Set<Integer> deleteCrawlerpicturesId) {
		crawlerpictureService.deleteCrawlerpictures(deleteCrawlerpicturesId);
		return "forward:/listCrawlerpictures";
	}

	@RequestMapping("/editCrawlerpicture")
	public ModelAndView editCrawlerpicture(@RequestParam Integer crawlerpictureId) {

		ModelAndView mav = new ModelAndView();

		Crawlerpicture crawlerpicture = crawlerpictureDAO.findCrawlerpictureByPrimaryKey(crawlerpictureId);
		// 将需要编辑的对象加到mav对象中
		mav.addObject("crawlerpicture", crawlerpicture);

		// 把所有的引用crawlerpicture对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerpictureService.loadCrawlerpictureObjectReferencedSet(crawlerpicture));

		// 将编辑页面crawlerpicture添加到mav中
		mav.setViewName("crawlerpicture/editCrawlerpicture");
		return mav;
	}

	@RequestMapping("/newCrawlerpicture")
	public ModelAndView newCrawlerpicture() {
		ModelAndView mav = new ModelAndView();
		Crawlerpicture crawlerpicture = new Crawlerpicture();

		mav.addObject("crawlerpicture", crawlerpicture);

		// 把所有的引用crawlerpicture对象都添加到mav对象，供前台页面选择
		mav.addAllObjects(crawlerpictureService.loadCrawlerpictureObjectReferencedSet(crawlerpicture));

		mav.addObject("newFlag", true);
		mav.setViewName("crawlerpicture/editCrawlerpicture");

		return mav;
	}

	@RequestMapping("/saveCrawlerpicture")
	public ModelAndView saveCrawlerpicture(@ModelAttribute Crawlerpicture crawlerpicture) {

		ModelAndView mav = new ModelAndView();

		Crawler crawlerTemp = crawlerpicture.getCrawler();
		Crawler crawler = crawlerTemp == null ? null
				: CmiHibernateProxyUtil.deproxy(crawlerDAO.findCrawlerById(crawlerTemp.getId()));

		Crawlerpicture existingCrawlerpicture = CmiHibernateProxyUtil.deproxy(
				crawlerpictureDAO.findCrawlerpictureByPhysicalPrimaryKey(crawlerpicture.getPath(), crawler.getId()));

		crawlerpicture.setCrawler(crawler);
		// 新建记录
		if (crawlerpicture.getId() == null) {
			if (crawlerpicture.equals(existingCrawlerpicture)) {

				mav.addAllObjects(crawlerpictureService.loadCrawlerpictureClassReferencedSet(crawlerpicture));
				// mav = editContinue(mav, crawlerpicture);
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerpicture/editCrawlerpicture");
			} else {
				crawlerpictureService.saveCrawlerpicture(crawlerpicture);
				mav.setViewName("forward:/listCrawlerpictures");
			}
		}
		// 编辑记录
		else {
			Crawlerpicture originCrawlerpicture = CmiHibernateProxyUtil
					.deproxy(crawlerpictureDAO.findCrawlerpictureByPrimaryKey(crawlerpicture.getId()));
			if (!crawlerpicture.equals(originCrawlerpicture) && crawlerpicture.equals(existingCrawlerpicture)) {
				mav.addAllObjects(crawlerpictureService.loadCrawlerpictureClassReferencedSet(crawlerpicture));
				mav.addObject("isInsertSuccess", "false");
				mav.setViewName("crawlerpicture/editCrawlerpicture");
			} else {
				crawlerpictureService.saveCrawlerpicture(crawlerpicture);
				mav.setViewName("forward:/listCrawlerpictures");
			}
		}
		return mav;
	}

	@RequestMapping("/crawlerpictureController/binary.action")
	public ModelAndView streamBinary(@ModelAttribute HttpServletRequest request,
			@ModelAttribute HttpServletResponse response) {
		ModelAndView mav = new ModelAndView();
		mav.setViewName("streamedBinaryContentView");
		return mav;

	}

}
